<template>
  <el-dialog
    :append-to-body="true"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="globalData.title"
    :visible.sync="isDialog.visible"
    top="7vh"
    width="1400px"
  >
    <div class="header-box">
      <!-- 商家 -->
      <div>
        <el-select
          v-model="queryInfo.provider_id"
          :loading="searchItem.provider_id.loading"
          :remote-method="handleSearchProvider"
          clearable
          filterable
          placeholder="请选择商家"
          remote
          reserve-keyword
          @change="handleResetPage"
        >
          <el-option
            v-for="item in searchItem.provider_id.options"
            :key="item.provider_id"
            :label="item.name + `(${item.account})`"
            :value="item.provider_id"
          >
          </el-option>
        </el-select>
      </div>
      <div>
        <el-button type="primary" @click="handleResetPage">
          搜索
          <i class="el-icon-search el-icon--right"></i>
        </el-button>
      </div>
      <div class="add">
        <el-button type="primary" @click="handleSubmit">确认订单</el-button>
      </div>
    </div>

    <div class="flex-content">
      <el-table
        ref="orderTable"
        v-loading="isLoading.table"
        :data="dataList"
        row-key="order_id"
        style="width: 100%"
        @selection-change="handleSelection"
      >
        <!-- 新增选择列 -->
        <el-table-column
          reserve-selection
          type="selection"
          width="55"
        ></el-table-column>

        <el-table-column align="center" label="订单号" prop="order_sn">
        </el-table-column>

        <el-table-column
          align="center"
          label="下单用户"
          prop="member"
          show-overflow-tooltip
          width="200"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="下单商家"
          prop="provider"
          show-overflow-tooltip
          width="200"
        >
        </el-table-column>

        <el-table-column align="center" label="订单总金额" prop="order_fee">
        </el-table-column>

        <el-table-column align="center" label="结算金额" prop="settle_cash">
        </el-table-column>

        <el-table-column align="center" label="付款情况" prop="payInfo">
        </el-table-column>

        <el-table-column align="center" label="完结时间" prop="complete_time">
        </el-table-column>

        <el-table-column
          align="center"
          label="备注"
          prop="remark"
          show-overflow-tooltip
        >
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页-->
    <div class="footer-box">
      <pagination
        :limit.sync="queryInfo.rows"
        :page.sync="queryInfo.page"
        :total="totalCount"
        @pagination="getData"
      >
      </pagination>
    </div>
  </el-dialog>
</template>

<script>
import { lists as getList } from "@/api/order";
import { lists as searchProvider } from "@/api/provider";

export default {
  name: "OrderSelect",
  props: {
    info: {
      type: Object,
    },
  },
  data() {
    return {
      // 查询条件
      queryInfo: {
        page: 1,
        rows: 100,
        bs_id: 1,
        provider_id: "",
        status: 6,
        r_id: 0,
      },
      // 统计
      dataList: [],
      // 总页数
      totalCount: 0,
      // 选中项
      selectedOrders: [],
      // 数据
      globalData: {
        title: "",
      },
      // 远程搜索项
      searchItem: {
        provider_id: {
          loading: false,
          options: [],
        },
      },
      // 加载
      isLoading: {
        table: false,
      },
      // 弹窗
      isDialog: {
        visible: true,
      },
      // 防抖
      queryId: +new Date(),
    };
  },
  created() {
    this.getData();
    this.handleSearchProvider();
    this.globalData.title = "选择订单";
  },
  methods: {
    // 详情
    getData() {
      this.isLoading.table = true;
      this.queryId++;
      let queryId = this.queryId;
      getList(this.queryInfo)
        .then((res) => {
          if (queryId !== this.queryId) {
            return;
          }
          this.dataList = res.data.list;
          this.totalCount = res.data.total;
        })
        .finally(() => {
          this.isLoading.table = false;
        });
    },
    // 选择
    handleSelection(selection) {
      this.selectedOrders = selection;
    },
    // 确定
    handleSubmit() {
      if (this.selectedOrders.length === 0) {
        this.$message({
          type: "info",
          message: "请选择订单",
        });
      } else {
        const selectedData = this.selectedOrders.map((item) => ({
          order_sn: item.order_sn,
          order_id: item.order_id,
        }));
        this.$emit("close", {
          selected: selectedData,
        });
      }
    },
    // 商品
    handleSearchProvider(query) {
      this.searchItem.provider_id.loading = true;
      searchProvider({
        page: 1,
        rows: 10,
        keywords: query ? query : "",
      }).then((res) => {
        this.searchItem.provider_id.loading = false;
        this.searchItem.provider_id.options = res.data.list;
      });
    },
    // 重置页面数据
    handleResetPage() {
      this.queryInfo.page = 1;
      this.getData();
    },
    // 关闭
    handleClose() {
      this.$emit("close", {});
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__body {
  padding: 0 20px 20px 20px;
}

.header-box {
  display: flex;

  > div {
    margin-right: 10px;
  }
}

.flex-content {
  margin-top: 10px;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;

    .title {
      font-size: 18px;
      font-weight: bold;
    }
  }
}
</style>
